package com.cloud.pikachu.face.service.impl;

import com.cloud.pikachu.face.dao.OrderFormDao;
import com.cloud.pikachu.face.po.TbOrderForm;
import com.cloud.pikachu.face.model.ReSaveOrderFormModel;
import com.cloud.pikachu.face.service.OrderFormService;
import org.reflections.util.Utils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 工单信息ServiceImpl
 */
@Service
public class OrderFormServiceImpl implements OrderFormService {
    private static final Logger logger = LoggerFactory.getLogger(OrderFormServiceImpl.class);
    @Autowired
    private OrderFormDao orderFormDao;

    @Override
    public String getOrderNewId(){
        String orderId = orderFormDao.getOrderNewId();
        if(Utils.isEmpty(orderId)){
            orderId = "1806-20161214-0001";
            logger.info("数据库中无工单信息，初始化工单编号为 : " + orderId);
        }
        return orderId;
    }

    @Override
    public ReSaveOrderFormModel saveOrderFormInfos(List<TbOrderForm> orderFormList){
        ReSaveOrderFormModel reModel = new ReSaveOrderFormModel();
        for(TbOrderForm orderForm : orderFormList){
            try{
                orderFormDao.saveOrderFormInfo(orderForm);
                reModel.getSucceeList().add(orderForm.getOrderId());
            }catch (Exception ex){
                logger.error(ex.getMessage());
                reModel.getFailureList().add(orderForm.getOrderId());
            }
        }
        return reModel;
    }
}